package com.smartgame.objects;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public abstract class AnimatedObject extends AbstractObject {
	
	protected Animation animation;
	protected TextureRegion[] animatedFrames;
	
	protected float stateTime;
	protected TextureRegion currentFrame;
	
	protected float frameDuration;
	protected int frameCols;
	protected int frameRows; 
	
	public AnimatedObject(Texture animatedTexture) {
		initAnimationWithTexture(animatedTexture);
	}
	
	private void initAnimationWithTexture(Texture animatedTexture) {
		initCoefficient();
		
        TextureRegion[][] tmp = TextureRegion.split(animatedTexture,
        		animatedTexture.getWidth() / frameCols, 
        		animatedTexture.getHeight() / frameRows);
        animatedFrames = new TextureRegion[frameCols * frameRows];
        
        int index = 0;
        for (int i = 0; i < frameRows; i++) {
            for (int j = 0; j < frameCols; j++) {
            	animatedFrames[index++] = tmp[i][j];
            }
        }
        animation = new Animation(frameDuration, animatedFrames);
	}
	
	/**
	 * initialize necessary coefficients, like follows<br>
	 * <ul> 
	 * <li>frameDuration
	 * <li>frameCols
	 * <li>frameRows
	 * </ul>
	 */
	protected abstract void initCoefficient();
	
}
